package cn.javacc.common.web;
/**
 * 
 * @author mzh2008@163.com 2012-11-12 下午11:26:48
 *
 */
public class ParamEncHelper {

	// 随机生成的密钥
	private static String keystr = null;
	// 有效的密码字符
	private static String encchars = "0123456789 ./`~!@#$%^&*()+|-=\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	// 随机生成密码串
	public synchronized static String getKey() {
		if (keystr != null)
			return keystr;
		// 随机生成32位密钥
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 32; i++) {
			int index = Math.round(Double.valueOf(Math.random() * encchars.length()).floatValue());
			if (index < 0 || index >= encchars.length()) {
				i--;
				continue;
			}
			sb.append(encchars.charAt(index));
		}
		keystr = sb.toString();
		return keystr;
	}

	public static String encodeHex(String content) {
		if (content == null || content.equals(""))
			return content;
		return EncUtils.encodeNormal(content, getKey());
	}

	public static String decodeHex(String content) {
		if (content == null || content.equals(""))
			return content;
		return EncUtils.decodeNormal(content, getKey());
	}

	/**
	 * 加密请求参数
	 * 
	 * @param url
	 * @return
	 */
	public static String encodeUrl(String url) {
		return EncUtils.encodeUrlNormal(url, getKey());
	}
}
